<?php

class Matching extends Controller {
	
	function Matching()
	{
		parent::Controller();
	}

	function index()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('StudentModel');
	    $this->load->library('pagination');

	    $per_page = 15;
   		$total = $this->StudentModel->countFroMatching();
   		
   
   		$base_url = site_url('Matching/index/');
	    $config['base_url'] = $base_url;
	    $config['total_rows'] = $total;
	    $config['per_page'] = $per_page;
	    $config['uri_segment'] = '3';
	    $this->pagination->initialize($config); 
		$data['paging'] = str_replace('index.html/', 'index/', $this->pagination->create_links());
		$data['selected_stu'] = $this->StudentModel->selectForMactching($per_page, $this->uri->segment(3));
		
		$this->load->view('header');
		$this->load->view('matching-list-form', $data);
		$this->load->view('footer');
	}
	
	function detail($id)
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
		$data['form_status'] = 'add';
		$this->load->model('StudentModel');
		$data['selected_stu'] = $this->StudentModel->selectForMactchingById($id);
		$this->load->view('header');
		$this->load->view('matching-list-form', $data);
		$this->load->view('footer');
	    
	}
	
	
	function confirm()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('StudentModel');
	    $studentArr = $this->input->post('txtDel');
	    $comArr = $this->input->post('comment');
		if(isset($studentArr) && empty($studentArr) == false) {
			for($i = 0; $i < count($studentArr); $i++) {
					$r = $studentArr[$i];
					$d = array();
					$tmps = explode(",", $r);
					$d['student_id'] = $tmps[0];
					$d['t_id'] = "'".$tmps[1]."'";
					$d['maching_status'] = 'อยู่ระหว่างการเรียนการสอน';
					if($tmps[2] != null && $comArr[$tmps[2]-1] != null)
						$d['note'] = $comArr[$tmps[2]-1];
					else 
						$d['note'] = '';
					$this->StudentModel->finishBooking($d);
				}
			}
	    $msg['msg'] = 'บันทึกข้อมูลเรียบร้อยแล้วค่ะ';
	    $this->index();
	}
	
	function reject()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('StudentModel');
	    $studentArr = $this->input->post('txtDel');
	    $comArr = $this->input->post('comment');
		if(isset($studentArr) && empty($studentArr) == false) {
				for($i = 0; $i < count($studentArr); $i++) {
					$r = $studentArr[$i];
					$d = array();
					$tmps = explode(",", $r);
					$d['student_id'] = $tmps[0];
					$d['maching_id'] = "'".$tmps[1]."'";
					$d['maching_status'] = 'อยู่ระหว่างการคัดเลือกติวเตอร์';
					if($tmps[2] != null && $comArr[$tmps[2]-1] != null)
						$d['note'] = $comArr[$tmps[2]-1];
					else 
						$d['note'] = '';
					$this->StudentModel->cancelBooking($d);
				}
			}
	    
	    $this->index();
	}

	
	
}

